#include "Acceptor.h"
#include "TcpConnection.h"
#include "EventLoop.h"
#include <iostream>
using std::cout;
using std::endl;

//由用户指定对三个事件的处理方式

//新的连接建立
void OnNewConnection(const TcpConnectionPtr & con)
{
    cout << con->toString() << " has connected! " << endl;
}


//客户端发送数据
void OnMessage(const TcpConnectionPtr & con)
{
    string msg = con->receive();
    cout << ">>recv msg from client: " << msg << endl;

    //处理接收到的数据
    /* msg = "msg:" + msg; */

    //处理后的数据发回客户端
    con->send(msg);
}


//连接断开
void OnClose(const TcpConnectionPtr & con)
{
    cout << con->toString() << " has closed! " << endl;
}


void test0(){
    Acceptor acceptor("127.0.0.1",8888);
    acceptor.ready();

    EventLoop eloop(acceptor);
    //传入函数名，隐式转换成function
    eloop.setNewConnectionCallback(OnNewConnection);
    eloop.setMessageCallback(OnMessage);
    eloop.setCloseCallback(OnClose);

    eloop.loop();
}

int main(void){
    test0();
    return 0;
}

